//
//  Utils.swift
//  ddx0513
//
//  Created by ejian  on 15/5/29.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import Foundation

class StringUtils {
    static let alipayErrorCode = ["9000": "订单支付成功", "8000": "正在处理中", "4000": "订单支付失败", "6001": "用户中途取消", "6002": "网络连接出错"]
    
    static let weixinPayErrorCode = [""]
    
    static let payChannel = ["weixin": "weixin", "alipay": "allipay"]
    
    static let WeipayNotification = "WeipayNotification"
    
    static let AlipayNotification = "AlipayNotification"
    
//    static let payType = ["oneMonth": "oneMonth", "sixMonths": "sixMonths", "oneYear": "oneYear", "cellphone": "cellphone"]

    
    //在字符串toString中插入新的字符串str， index从0开始
    class func insertAtIndex(str: String, toString: String, atIndex index: Int) -> String {
        var length = count(toString)
        
        if (index < 0 || index > length) {
            return toString
        } else if (index == 0) {
            return str + toString
        } else if (index == length) {
            return toString + str
        } else {
            let range1 = Range(start: toString.startIndex, end: advance(toString.startIndex, index))
            let range2 = Range(start: advance(toString.startIndex, index), end: toString.endIndex)
            var str1 = toString.substringWithRange(range1)
            var str2 = toString.substringWithRange(range2)
            
            return str1 + str + str2
        }
    }
    
    //将url类参数拆分成key、value数组
    class func splitTranStr(str: String) -> [String: String] {
        let arr1 = str.componentsSeparatedByString("&")
        var ret: [String: String] = [String: String]()
        for v in arr1 {
            let arr2 = v.componentsSeparatedByString("=")
            var s = ""
            for var i = 1;i < arr2.count; i++ {
                if (i == 1) {
                    s = s + arr2[i]
                } else {
                    s = s + "=\(arr2[i])"
                }
            }
            ret[arr2[0]] = s
        }
        
        return ret
    }
    
    
    //将字节数组转换成十六进制字符串
    class func getHexStrFromByteArray(byteArray: [Byte]) -> String {
        var hexStr: String = ""
        for (var i = 0; i < byteArray.count; i++) {
            var newHexStr = NSString(format: "%x", byteArray[i]&0xff)
            if (newHexStr.length == 1) {
                hexStr = hexStr + "0" + (newHexStr as String)
            } else {
                hexStr = hexStr + (newHexStr as String)
            }
        }
        
        return hexStr
    }
    
    //将UIImage转换成十六进制字符串
    class func getHexStrFromImage(image: UIImage) -> String {
        //转换成NSData
        var imageData = UIImageJPEGRepresentation(image, 0.5)
        
        //转换成byte数组
        var byteArray: [Byte] = [Byte]()
        for i in 0..<imageData.length {
            var temp: Byte = 0
            imageData.getBytes(&temp, range: NSRange(location: i, length: 1))
            byteArray.append(temp)
        }
        
        return getHexStrFromByteArray(byteArray)
        
    }
    
    //从毫秒转换成时间
    class func getDateString(time: Double, format: String) -> String {
        var date = NSDate(timeIntervalSince1970: time/1000)
        var dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.stringFromDate(date)
    }
    
    //两个毫秒时间之间的天数
    class func getDayCount(fromTime: Double, toTime: Double) -> Int {
        return Int((toTime - fromTime)/1000/3600/24)
    }
    
    //获取播放时长秒数
    class func getSeconds(time: String) -> Int {
        if (time == "") {
            return 0
        }
        
        var array = time.componentsSeparatedByString(":")
        switch (array.count) {
        case 1:
            return array[0].toInt()!
        case 2:
            return array[0].toInt()! * 60 + array[1].toInt()!
        case 3:
            return array[0].toInt()! * 3600 + array[1].toInt()! * 60 + array[2].toInt()!
        default:
            return 0
        }
    }
    
    //格式化播放时长，如1:23:45
    class func getFormatPlayTime(time: Int) -> String {
        if (time < 0) {
            return "00:00"
        }
        
        var s = ""
        let hour = time / 3600
        if (hour > 0) {
            s = s + String(hour) + ":"
        }
        
        let minute = (time - 3600 * hour) / 60
        if (minute < 10) {
            s = s + "0" + String(minute) + ":"
        } else {
            s = s + String(minute) + ":"
        }
        
        let second = time - 3600 * hour - 60 * minute
        if (second < 10) {
            s = s + "0" + String(second)
        } else {
            s = s + String(second)
        }
        
        return s
    }
    
    //格式化文件或文件夹大小，返回123K、12.3M或12.3G
    class func formatFileSize(sizeInB: Int64) -> String {
        let sizeInKB = sizeInB / 1000
        if (sizeInKB < 1000) {
            return "\(sizeInKB)K"
        } else if (sizeInKB >= 1000 && sizeInKB < 1000000) {
//            let sizeInMB = sizeInKB / 1000
            return String(format: "%.1fM", Float(sizeInKB) / 1000)
        } else {
//            let sizeInGB = sizeInKB / 1000000
            return String(format: "%.1fG", Float(sizeInKB) / 1000000)
        }
    }
    
}